<?php
// 商品细节页
class DetailAction extends CommonAction{
	private $gid;//商品gid
	public function __auto(){
		$this->gid=$this->_param('gid');
	}
	public function index(){
		$goods=$this->getOne($this->gid);
		$goods=$this->disDetailData($goods);
		$this->assign('goods',$goods);
		$this->display();
	}
	// 获得单条商品数据及细节
	public function getOne($gid){
			$db=M();

			$sql="select g.*,l.*,sh.*,c.*,d.* from group_goods g 
					left join group_goods_detail d on d.goods_id=g.gid
					left join group_locality l on l.lid=g.lid
					left join group_shop sh on sh.shopid=g.shopid
					left join group_category c on c.cid=g.cid
					where g.gid={$gid}
			 ";
			$result=$db->query($sql);
			return $result[0];
	}

	 // 商品数据处理
	private function disDetailData($detail){
		// 处理折扣
		$detail['zekou']=number_format(($detail['price'])/$detail['old_price']*10,1);//取小数点后一位php函数number_format

		// 处理图片名称
		$detail['goods_img']=explode('.',$detail['goods_img'])[0];//去掉保存的图片后缀，模版中重新加图片大小重新组合新图片名

		// 处理倒计时天数
		if(($detail['end_time']-time())>(pow(60,2)*24*3)){//倒计时时间
			$detail['time']='剩余<span>3</span>天以上';
		}else{
			$detail['time']=date('Y-m-d H:i:s',$detail['end_time']).'下架';
		}

		//处理商品服务
		$serverArr=unserialize($detail['goods_server']);// 反序列化商品服务
		$slice=array_slice($serverArr,0,2);//截取数组的前两个元素
		$serverC=C('goods_server');//获取配置项里的goods_server
		$detail['goods_server']=array();
		$detail['goods_server'][]=$serverC[$slice[0]];
		$detail['goods_server'][]=$serverC[$slice[1]];
		return $detail;
	}
}
?>